package com.server.impl;

import com.dao.menu.MenuMapper;
import com.pojo.Menu;
import com.pojo.MenuBean;
import com.server.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    MenuMapper menuMapper;

    @Override
    public List<Menu> getAllMenus(int rolePid) {
        return menuMapper.getAllMenus(rolePid);
    }

    @Override
    public List<MenuBean> getMenuList(int rolePid) {
        List<MenuBean> menuBeans=new ArrayList<>();
        List<Menu> mainMenu=menuMapper.getMainMenus(rolePid);//获取某角色下的主菜单

        for (Menu menu: mainMenu) {//遍历主菜单列表，分别查询主菜单下对应的子菜单
            MenuBean menuBean=new MenuBean();
            menu.setRolePid(rolePid);
            System.out.println(menu.getRolePid());
            List<Menu> subMenu=menuMapper.getSubMenus(menu);
            menuBean.setMainMenu(menu);//设置菜单项额主菜单
            menuBean.setSubMenu(subMenu);//设置菜单项的主菜单
            menuBeans.add(menuBean);//把主菜单项加到列表中
        }
        return menuBeans;
    }
}
